Fix message_buffer under-allocation in receive_messages#305
Open
caniko wants to merge 2 commits intoNoxime:masterfrom
Open
Fix message_buffer under-allocation in receive_messages#305caniko wants to merge 2 commits intoNoxime:masterfrom
caniko wants to merge 2 commits intoNoxime:masterfrom
Conversation
Currently `send_messages()` is only available on `ListenSocket`, but the underlying C++ API `ISteamNetworkingSockets::SendMessages()` is a top-level interface method that works with any connection handle. This means P2P dial-side connections (from `connect_p2p()`) have no way to use the efficient batched send path with per-message channel/lane tagging — the dialer never gets a `ListenSocket`, only a `NetConnection`. This is needed to use connection lanes (`ConfigureConnectionLanes`) from both sides of a P2P connection. Without it you can configure lanes but only the listener can actually send on them. The implementation is identical to `ListenSocket::send_messages()` — both structs hold the same `*mut ISteamNetworkingSockets` pointer, just in different fields. Closes Noxime#302
`Vec::reserve(additional)` ensures `capacity >= len + additional`, not `capacity >= capacity + additional`. When the buffer had some leftover capacity (0 < capacity < batch_size), the old expression `reserve(batch_size - capacity)` would guarantee only `capacity >= batch_size - old_capacity`, which is less than batch_size. The Steam API could then write past the allocated region. Pass `batch_size` directly so the guarantee becomes `capacity >= 0 + batch_size = batch_size`, which is correct since the buffer is always drained between calls (len == 0).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a buffer under-allocation bug in
NetConnection::receive_messages,receive_messages_into, andNetPollGroup::receive_messages.Why
The old code did
reserve(batch_size - capacity), butVec::reserve(additional)guaranteescapacity >= len + additional— notcapacity + additional. When the internal message buffer had some leftover capacity but less thanbatch_size, the reservation fell short. The Steam API could then write messages beyond the allocated region, causing undefined behavior.Since the buffer is always drained between calls (
len == 0), passingbatch_sizedirectly gives the correct guarantee:capacity >= batch_size.Changes
Three call sites, same one-line fix each — replaced
reserve(batch_size - capacity)withreserve(batch_size).